package com.company.project.service.impl;

import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.dao.SearchMapper;
import com.company.project.dao.ShopInfoMapper;
import com.company.project.dao.ShopLoginMapper;
import com.company.project.dao.ShopMapper;
import com.company.project.model.*;
import com.company.project.model.request.QueryParams;
import com.company.project.model.response.PageList;
import com.company.project.service.ShopService;
import com.company.project.core.AbstractService;
import com.company.project.utils.ImageBinary;
import com.company.project.utils.MD5Util;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by CodeGenerator on 2020/04/21.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopServiceImpl extends AbstractService<Shop> implements ShopService {
    @Resource
    private ShopMapper shopMapper;
    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private ShopLoginMapper shopLoginMapper;
    @Autowired
    private ShopInfoMapper shopInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result getHotSearch() {
        List<Search> list = searchMapper.getHotSearch();
        return ResultGenerator.genSuccessResult(list);
    }

    @Override
    public Result getSearching(String content) {
        List<Search> list = searchMapper.getSearching(content);
        return ResultGenerator.genSuccessResult(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addSearchNum(String id) {
        Search search = searchMapper.selectByPrimaryKey(id);
        if (search == null) {
            return ResultGenerator.genFailResult("不存在");
        }
        search.setNum(search.getNum() + 1);
        searchMapper.updateByPrimaryKey(search);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result shopRegister(ShopLogin shop) {
        String code = (String) redisTemplate.opsForValue().get(shop.getEmail());
        if (!shop.getCode().equals(code)) {
            return ResultGenerator.genFailResult("邮箱验证码错误");
        }
        shop.setStatus("1");
        shop.setCreateTime(new Date());
        // 密码加密
        shop.setPassword(MD5Util.md5(shop.getPassword(), shop.getEmail()));
        shopLoginMapper.insert(shop);
        ShopLogin shopLogin = shopLoginMapper.selectOne(shop);
        return ResultGenerator.genSuccessResult(shopLogin);
    }

    @Override
    public Result editShopDetail(ShopInfo shopInfo) {
        ShopLogin query = new ShopLogin();
        query.setShopId(shopInfo.getShopId());
        ShopLogin entity = shopLoginMapper.selectOne(query);

        Shop shop = new Shop();
        shop.setShopId(shopInfo.getShopId());
        shop.setShopName(entity.getName());
        shop.setAddress(shopInfo.getAddress());
        shop.setShopKind(shopInfo.getShopKind());
        String projectPath = System.getProperty("user.dir");
        if (StringUtils.isNotBlank(shopInfo.getLogoImg())) {
            String filePath = projectPath + "\\src\\main\\resources\\static\\" + shopInfo.getLogoImg();
            String img = ImageBinary.getImageBinary(filePath);
            shop.setLogoImg(img);
        }
        shopMapper.insert(shop);

        shopInfo.setShopName(entity.getName());

        List<Date> timeList = shopInfo.getTime();
        shopInfo.setBeginTime(timeList.get(0));
        shopInfo.setEndTime(timeList.get(1));

        if (StringUtils.isNotBlank(shopInfo.getStoreImg())) {
            String filePath = projectPath + "\\src\\main\\resources\\static\\" + shopInfo.getStoreImg();
            String img = ImageBinary.getImageBinary(filePath);
            shopInfo.setLogoImg(img);
        }
        if (StringUtils.isNotBlank(shopInfo.getLogoImg())) {
            String filePath = projectPath + "\\src\\main\\resources\\static\\" + shopInfo.getLogoImg();
            String img = ImageBinary.getImageBinary(filePath);
            shopInfo.setStoreImg(img);
        }
        shopInfoMapper.insert(shopInfo);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result getShopList(QueryParams queryParams) {
        int page = queryParams.getPage();
        int size = queryParams.getSize();
        if (queryParams.getTime() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            queryParams.setTimeString(dateFormat.format(queryParams.getTime()));
        }
        queryParams.setStart((page - 1) * size);
        List<ShopLogin> list = shopMapper.getShopList(queryParams);
        Integer total = shopMapper.getShopListTotal(queryParams);
        PageList<ShopLogin> pageList = new PageList<>();
        pageList.setList(list);
        pageList.setTotal(total);
        return ResultGenerator.genSuccessResult(pageList);
    }

    @Override
    public Shop findShopById(Integer shopId) {
        return shopMapper.findShopById(shopId);
    }

    @Override
    public Result deleteAll(List<Shop> shopList) {
        for (Shop shop : shopList) {
            ShopLogin query = new ShopLogin();
            query.setShopId(shop.getShopId());
            ShopLogin entity = shopLoginMapper.selectOne(query);
            entity.setStatus("2");
            shopLoginMapper.updateByPrimaryKey(entity);
        }
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result export(List<ShopLogin> shopList) {
        List<Shop> list = new ArrayList<>();
        for (ShopLogin shop : shopList) {
            Shop item = shopMapper.findExportShop(shop.getShopId());
            if (item != null) {
                list.add(item);
            }
        }
        return ResultGenerator.genSuccessResult(list);
    }

    @Override
    public Shop selectOne(Shop query) {
        return shopMapper.selectOne(query);
    }

    @Override
    public List<Shop> findList(String kind, Integer price, Integer price2) {
        return shopMapper.findList(kind, price, price2);
    }
}
